package com.feiyu.file.util;

import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * @author tang ming
 * @since 2021/12/6 17:32
 */
public class FileUtil {

    public static FastFile covert(MultipartFile file) throws IOException {
        InputStream inputStream=file.getInputStream();
        String fileName=file.getOriginalFilename();
        String ext=fileExtName(fileName);
        long size= file.getSize();
        Set<MetaData> metaDataSet=new HashSet<>();
        metaDataSet.add(new MetaData("uploader","tang ming"));
        return new FastFile(inputStream,size,ext,metaDataSet);
    }

    public static String fileExtName(String fileName){
        if (fileName==null)
            return null;
        int index=fileName.lastIndexOf('.');
        return fileName.substring(index+1);
    }
}
